package com.ren.fm.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ren.fm.entity.*;
import com.ren.fm.entity.vo.ReclFmQueryVo;
import com.ren.fm.entity.vo.ReclFmVo;
import com.ren.fm.service.ReclFmCollectionService;
import com.ren.fm.service.ReclFmCommentService;
import com.ren.fm.service.ReclFmPraiseService;
import com.ren.fm.service.ReclFmService;
import com.ren.utils.page.PageItem;
import com.ren.utils.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: ReclFmFrontController
 * @Description: TODO
 * @Author: RZY
 * @DATE: 2022/7/13 14:09
 * @Version: v1.0
 */

@Api(tags = "FM前台接口")
@RestController
@RequestMapping("/recl-fm/front")
public class ReclFmFrontController {

    @Autowired
    ReclFmService fmService;

    @Autowired
    ReclFmPraiseService fmPraiseService;

    @Autowired
    ReclFmCommentService fmCommentService;

    @Autowired
    ReclFmCollectionService fmCollectionService;

    @ApiOperation(value = "查询指定用户点赞的所有FM")
    @GetMapping("/fm-detail-user/{userId}")
    public R getFmByPraiseUserId(@PathVariable Long userId) {
        List<ReclFmVo> reclFms = fmService.getFmByPraiseUserId(userId);
        return R.ok()
                .data("reclFms", reclFms);
    }

    @ApiOperation(value = "查询指定用户评论的所有FM")
    @GetMapping("/fm-detail-comment/{userId}")
    public R getFmByCommentUserId(@PathVariable Long userId) {
        List<ReclFmVo> reclFms = fmService.getFmByCommentUserId(userId);
        return R.ok()
                .data("reclFms", reclFms);
    }

    @ApiOperation(value = "查询指定用户所有收藏的FM")
    @GetMapping("/fm-detail-collection/{userId}")
    public R getFmByCollectionUserId(@PathVariable Long userId) {
        List<ReclFmVo> reclFms = fmService.getFmByCollectionUserId(userId);
        return R.ok()
                .data("reclFms", reclFms);
    }

    @ApiOperation(value = "查询前台FM数据分页")
    @PostMapping("/{current}/{limit}")
    public R getFrontFmOfPage(@RequestBody(required = false) ReclFmQueryVo reclFmQueryVo,
                         @PathVariable long current,
                         @PathVariable long limit) {
        PageItem<ReclFmVo> fmPageItem = fmService.getFrontFmOfPage(reclFmQueryVo, current, limit);
        return R.ok()
                .data("records", fmPageItem.getPageRecords())
                .data("total", fmPageItem.getTotal());
    }

    @ApiOperation(value = "添加FM点赞")
    @PostMapping("/fm-praise")
    public R addFmPraise(@RequestBody ReclFmPraise reclFmPraise) {
        boolean res = fmPraiseService.saveOrUpdate(reclFmPraise);
        if(res) return R.ok()
                .message("感谢你的支持！");
        else return R.error()
                .message("点赞失败，请稍后再试！");
    }

    @ApiOperation(value = "添加FM评论")
    @PostMapping("/fm-comment")
    public R addFmComment(@RequestBody ReclFmComment reclFmComment) {
        boolean res = fmCommentService.saveOrUpdate(reclFmComment);
        if(res) return R.ok()
                .message("评论成功！");
        else return R.error()
                .message("评论失败，请稍后再试！");
    }

    @ApiOperation(value = "添加FM收藏")
    @PostMapping("/fm-collection")
    public R addFmCollection(@RequestBody ReclFmCollection reclFmCollection) {
        boolean res = fmCollectionService.saveOrUpdate(reclFmCollection);
        if(res) return R.ok()
                .message("感谢你的支持！");
        else return R.error()
                .message("收藏失败，请稍后再试！");
    }

    @ApiOperation(value = "查询指定FM的所有评论")
    @PostMapping("/fm-comment/{fmId}")
    public R addFmComment(@PathVariable Long fmId) {
        List<ReclFmComment> reclFmComments = fmCommentService.list(new QueryWrapper<ReclFmComment>()
                .eq("fm_id", fmId));
        return R.ok()
                .data("reclFmComments", reclFmComments);
    }
}
